function compare_results1(netName, scale, varargin)  % made by myself
% compare_results(netName, scale, GroundTruth, Bicubic, SR1, SR1+VDSR)
while(iscell(varargin{1})), varargin = varargin{1}; end

if ~isstruct(varargin{1})
    rgbList = varargin;
else
    FS = {'SRCNN', 'FSRCNN', 'VDSR0','RED',  'DRRN'};
    N = length(fields(varargin{1}));
    rgbList = cell(2, N/2);
    rgbList(1:2, 1) = {varargin{1}.HR; varargin{1}.Bicubic};
    for i = 1 : length(FS)
        rgbList(1:2, i+1) = {varargin{1}.(FS{i}); varargin{1}.([FS{i} '_DRLN'])};
    end
    rgbList = vec(rgbList');
end
clear varargin;

imsize = size(rgbList{1});
rgbList = cellfun(@(x) norm_size(x, imsize), rgbList, 'uniformoutput', false);
% rgbList = rgbList([2, 1, 3 : end]);
% rgbList = rgbList([1 : 2 : end, 2 : 2 : end]);
% rgbList = cellfun(@ myycbcr2rgb, varargin, 'uniformoutput', false);
grayList = cellfun(@ myrgb2gray, rgbList, 'uniformoutput', false);

hPSNR = @(x)mytool.compute_psnr(x,grayList{1});
hSSIM = @(x)mytool.ssim_index(x,grayList{1});
qualities = zeros(2, length(rgbList));
qualities(1, 2:end) = cell2num(cellfun(hPSNR, grayList(2:end), 'uniformoutput', false));
qualities(2, 2:end) = cell2num(cellfun(hSSIM, grayList(2:end), 'uniformoutput', false));

% subplot(1,2,1); hres1 = imshow(abs(grayList{1} - varargin{3}(:,:,1)), []);
% subplot(1,2,2); hres2 = imshow(abs(grayList{1} - varargin{4}(:,:,1)), []);
figure; hres = imshow(abs(grayList{length(rgbList)/2} - grayList{end}), []);
colormap hot; axis2 = gca;

notes = sprintf([
    '1. ���������ѡ�����Ȥ������\n'...
    '2. �������Ҽ�ر����дӸô��ڴ򿪵�figure\n'...
    '3. �����Shift+���������»������и���Ȥ������\n'...
    '4. ����������еġ����桱��ť�����沢�رո�figure��\n'...
    '   ����˫���򿪣���������и���Ȥ�����SR�Ա�ͼ'
    ]);
text(-size(grayList{1}, 2), 10, notes);

setappdata(hres, 'netName', netName);
% setappdata(hres, 'grayList', grayList);
setappdata(hres, 'rgbList', rgbList);
setappdata(hres, 'qualities', qualities);

set(hres, 'ButtonDownFcn', @(hObject, eventdata) myButtonDownFcn(hres, axis2));

% hcmenu = uicontextmenu;
% uimenu(hcmenu, 'Label', 'Close All', 'Callback', @(x) save_fig); % �ַ���ʽ
% set(hres, 'uicontextmenu', hcmenu)

function x = norm_size(x, imsize)
if any(size(x) ~= imsize), imresize(x, imsize); end

function x = myycbcr2rgb(x)
if ndims(x) == 3, x = ycbcr2rgb(x); end

function x = myrgb2gray(x)
if ndims(x) == 3, x = rgb2ycbcr(x); end  % changed by wu, the original :ndims(x) == 3, x = rgb2ycbcr(double(x));
x = x(:, :, 1);

function myButtonDownFcn(hres, haxis)
netName = getappdata(hres, 'netName');
rgbList = getappdata(hres, 'rgbList');
qualities = getappdata(hres, 'qualities');
switch (get(gcf, 'SelectionType'))
    case 'normal'
        imshow_selected_block(hres, haxis, netName, rgbList, qualities);
    case 'extend' % 'Shift+������ͬʱ�����Ҽ�';
        grayList = cellfun(@ myrgb2gray, rgbList, 'uniformoutput', false);
%         hPSNR = @(x) srimg.psnr(grayList{1}, x, scale);
        hSSIM = @(x) srimg.ssim(grayList{1}, x, 4);
%         qualities = zeros(2, length(varargin));
%         qualities(1, 2:end) = cell2num(cellfun(hPSNR, grayList(2:end), 'uniformoutput', false));
        qualities(2, 2:end) = cell2num(cellfun(hSSIM, grayList(2:end), 'uniformoutput', false));
        
        [~, rects] = get_rects(hres);
        for i = 1 : size(rects, 1)
            imshow_selected_block(hres, haxis, netName, rgbList, qualities, rects(i, :))
        end
    case 'alt' % 'Ctrl+�����ߵ����Ҽ�';
        figs = getappdata(hres, 'figs');
        if isempty(figs), return; end
        idx = cell2num(arrayfun(@(h)h.isvalid, figs, 'UniformOutput', false));
        figs(idx == 0) = [];
        close(figs);
        setappdata(hres, 'figs', []);
    case 'open' % '˫����������';
end

function imshow_selected_block(hres, haxis, netName, rgbList, qualities, pos)
if nargin < 6
    h = imrect(haxis);
    if isempty(h), return; end
    pos = round(h.getPosition);  % h.delete;
    [imrects, rects] = get_rects(hres);
    
    imrects = [imrects h];
    rects = [rects; pos];
    setappdata(hres, 'imrects', imrects);
    setappdata(hres, 'rects', rects);
end

fig_gnd = figure; imshow(rgbList{1});
rectangle('Position', pos, 'EdgeColor', 'r', 'LineWidth', 3);

pp1 = pos(2) : pos(2) + pos(4);
pp2 = pos(1) : pos(1) + pos(3);

fig_patch = figure;
c = 0;
haxes = tight_subplot(2, length(rgbList)/2, .009, .009, .009);
for i = 1 : length(rgbList)
    c = c + 1;
    x = rgbList{i}(pp1, pp2, :);
    fig_patch.CurrentAxes = haxes(i); imshow(x);
    % axes(haxes(i)); imshow(x);
    set(gca, 'fontsize', 7);
    if i > 1 && isempty(netName)
        xlabel(sprintf('(%.2f,%.4f)', qualities(:, i)));
        continue;
    end
    
    switch i
        case 1
            xlabel('HR (PSNR, SSIM)');
        case 2
            xlabel(sprintf('Bicubic (%.2f,%.4f)', qualities(:, i)));
        case 3
            xlabel(sprintf('%s (%.2f,%.4f)', netName, qualities(:, i)));
        case 4
            xlabel(sprintf('%s+DRLN (%.2f,%.4f)', netName, qualities(:, i)));
    end
end

figs = getappdata(hres, 'figs');
figs = [figs fig_gnd fig_patch];
setappdata(hres, 'figs', figs);

function [imrects, rects] = get_rects(hres)
imrects = getappdata(hres, 'imrects');
rects = getappdata(hres, 'rects');

if isempty(imrects), return; end

idx = cell2num(arrayfun(@(h)h.isvalid, imrects, 'UniformOutput', false));
imrects(idx == 0) = [];
% rects(idx == 0, :) = [];
rects = arrayfun(@(h)h.getPosition, imrects, 'UniformOutput', false);
rects = round(cat(1, rects{:}));
